/*
 * $URL: https://dnsprobe.googlecode.com/svn/trunk/dnsprobe/src/dnsprobe/resolver/ResolverTask.java $
 * 
 * Copyright (c) 2009, Anonymous
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Anonymous nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY ANONYMOUS ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL ANONYMOUS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

package dnsprobe.resolver;

import java.net.InetAddress;
import java.net.UnknownHostException;

import dnsprobe.net.domain.Hostname;
import dnsprobe.net.domain.lookup.LookupResult;
import dnsprobe.net.domain.lookup.LookupResultNonExistent;
import dnsprobe.net.domain.lookup.LookupResultRecord;


/**
 * Loest einen Hostnamen ueber 2 Nameserver auf und vergleicht die Ergebnisse
 * 
 * @author BerndBernhardson
 * @author anon004
 * @author snoop
 * @version $Id: ResolverTask.java 118 2009-07-01 21:32:08Z bernhardson@gmx.net $
 * 
 * @see ResolverThread#startResolver()
 */
public class ResolverTask implements Runnable
{
	private final Hostname   hostname;
	
	private final Nameserver openNameserver;
	private final Nameserver userNameserver;
	
	private final ResolverThread parent;
	private final Statistics     statistics;
	
	/**
	 * Konsturktor
	 * 
	 * @param hostname       Der aufzulosende Hostname
	 * @param openNameserver Der zu benutzende offene Nameserver
	 * @param userNameserver Der zu benutzende gefilterte Nameserver
	 */
	public ResolverTask(final Hostname hostname, final Nameserver openNameserver, final Nameserver userNameserver, final ResolverThread parent)
	{
		this.hostname = hostname;
		
		this.openNameserver = openNameserver;
		this.userNameserver = userNameserver;
		
		this.parent     = parent;
		this.statistics = parent.getStatistics();
	}
	
	/**
	 * Loest einen Hostnamen auf und speichert das Ergebnis in einem 
	 * LookupResult-Objekt
	 * 
	 * @param hostname   Der aufzuloesende Hostname
	 * @param nameserver Der zu benutzende Nameserver
	 * 
	 * @return LookupResultRecord wenn der Host gefunden wurde, 
	 *         LookupResultNonExistent wenn nicht
	 */
	private LookupResult resolveHostname(final String hostname, final Nameserver nameserver)
	{
		LookupResult lookupResult;
		
		try
		{
			final InetAddress[] inetAddresses;
			
			inetAddresses = nameserver.lookupAllHostAddr(hostname);
			
			lookupResult = new LookupResultRecord();
			
			for(final InetAddress inetAddress: inetAddresses)
			{
				lookupResult.addAddress(inetAddress);
			}
			
		}
		catch(UnknownHostException e)
		{
			lookupResult = new LookupResultNonExistent();
		}
		
		return lookupResult;
	}
	
	/**
	 * Loest einen Hostnamen ueber 2 Nameserver auf und vergleicht 
	 * die Ergebnisse
	 */
	@Override
	public void run()
	{
		//----------------------------------------------------------------------
		// open dns aufloesen
		//----------------------------------------------------------------------
		parent.printLog("Resolving " + hostname.toString() + " using Open DNS...");
		hostname.setOpen(resolveHostname(hostname.toString(), openNameserver));
		if(hostname.getOpen() instanceof LookupResultRecord)
		{
			statistics.incOpenFound();
		}
		else
		{
			statistics.incOpenNotFound();
		}
		
		statistics.incResolveProgress();
		
		
		//----------------------------------------------------------------------
		// user dns aufloesen
		//----------------------------------------------------------------------
		parent.printLog("Resolving " + hostname.toString() + " using Your DNS...");
		hostname.setRestricted(resolveHostname(hostname.toString(), userNameserver));
		
		if(hostname.getRestricted() instanceof LookupResultRecord)
		{
			statistics.incUserFound();
		}
		else
		{
			statistics.incUserNotFound();
		}
		
		parent.getStatistics().incResolveProgress();
		
		
		//----------------------------------------------------------------------
		// vergleichen
		//----------------------------------------------------------------------
		parent.printLog("Comparing lookup results...");
		hostname.compareLookupResult();
		
		if(!hostname.hasDifferences())
		{
			statistics.incEqual();
		}
		else
		{
			statistics.incDifferent();
		}
		
		parent.getStatistics().incCompareProgress();
		
		parent.updateStatistics();
		parent.done();
	}
	
	
	
}
